package com.shop.shopmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.shopmall.mapper.GoodsInfoMapper;
import com.shop.shopmall.pojo.GoodsAttrValue;
import com.shop.shopmall.pojo.GoodsInfo;
import com.shop.shopmall.service.GoodsInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author jamie
 * @description 针对表【goods_info】的数据库操作Service实现
 * @createDate 2025-03-31 17:10:38
 */
@Service
public class GoodsInfoServiceImpl extends ServiceImpl<GoodsInfoMapper, GoodsInfo>
        implements GoodsInfoService {

    @Autowired
    GoodsInfoMapper goodsInfoMapper;

    @Autowired
    GoodsAttrValueServiceImpl goodsAttrValueService;

    @Override
    public IPage<GoodsInfo> getGoodsInfoList(GoodsInfo goodsInfo) {
        // 设置分页内容
        IPage<GoodsInfo> page = new Page<>(goodsInfo.getPageNum(), goodsInfo.getPageSize());
        LambdaQueryWrapper<GoodsInfo> wrapper = new LambdaQueryWrapper<>();
        // 设置查询条件
        if (!StringUtils.isEmpty(goodsInfo.getGoodsName())) {
            wrapper.eq(GoodsInfo::getGoodsName, goodsInfo.getGoodsName());
        }
        if (null != goodsInfo.getClassifyIds() && goodsInfo.getClassifyIds().length > 0) {
            wrapper.in(GoodsInfo::getClassify, Arrays.stream(goodsInfo.getClassifyIds()).toList());
        }
        // TODO 根据tabs查询不同的数据
        // 出售中
        if (goodsInfo.getTabs().equals("1")) {
            wrapper.eq(GoodsInfo::getStatus, "1");
        }
        // 仓库中
        if (goodsInfo.getTabs().equals("2")) {
            wrapper.eq(GoodsInfo::getStatus, "0");
        }
        // 已售罄
        if (goodsInfo.getTabs().equals("3")) {
            List<GoodsAttrValue> goodsAttrValueList = goodsAttrValueService.list(new LambdaQueryWrapper<GoodsAttrValue>()
                    .eq(GoodsAttrValue::getStock, 0));
            // 判断是否存在售罄商品
            if (goodsAttrValueList.isEmpty()) {
                return null;
            }
            // 获取库存为0的商品信息
            for (GoodsAttrValue goodsAttrValue : goodsAttrValueList) {
                wrapper.eq(GoodsInfo::getId, goodsAttrValue.getGoodsId());
            }
        }
        // 警戒库存
        if (goodsInfo.getTabs().equals("4")) {
            // 获取库存小于等于10的商品信息
            List<GoodsAttrValue> goodsAttrValueList = goodsAttrValueService.list(new LambdaQueryWrapper<GoodsAttrValue>()
                    .le(GoodsAttrValue::getStock, 10));
            // 判断是否存在警戒库存商品
            if (goodsAttrValueList.isEmpty()) {
                return null;
            }
            for (GoodsAttrValue goodsAttrValue : goodsAttrValueList) {
                wrapper.eq(GoodsInfo::getId, goodsAttrValue.getGoodsId());
            }
        }
        // 回收站
        if (goodsInfo.getTabs().equals("5")) {
            wrapper.eq(GoodsInfo::getIsRecycle, "1");
        }
        return this.baseMapper.selectPage(page, wrapper);
    }

    @Override
    public Map<String, Integer> getGoodsTotal() {
        return goodsInfoMapper.getGoodsTotal();
    }

}




